package com.hdjk.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.hdjk.view.R;


public class TextButtonDialog extends Dialog {

	private View.OnClickListener mListener = null;
	private TextView mTvContent = null;
	private TextView mTvSubContent = null;
	private TextView mBtnCancel = null;
	private TextView mBtnOk = null;
	private String mContent = "";
	private String mBtnCancelText="取消";
	private String mBtnOkText="确定";
	private boolean isSingleButton = false;
	private Object mData = null;
	private View vLineV, vLineH;

	public TextButtonDialog(@NonNull Context context,View.OnClickListener listener) {
		super(context);
		mListener = listener;
	}

	public TextButtonDialog(Context context, int theme,
							View.OnClickListener listener) {
		super(context, theme);
		mListener = listener;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_text_buttons);

		mTvContent = (TextView) findViewById(R.id.dialog_content);
		mTvContent.setText(mContent);
		mBtnCancel = (TextView) findViewById(R.id.tv_cancel);
		mBtnCancel.setText(mBtnCancelText);
		mBtnOk = (TextView) findViewById(R.id.tv_sure);
		mBtnOk.setText(mBtnOkText);
		mBtnCancel.setOnClickListener(mListener);
		mBtnOk.setOnClickListener(mListener);

		mTvSubContent = (TextView) findViewById(R.id.dialog_sub_content);
		
		vLineV = findViewById(R.id.vLineV);
		vLineH = findViewById(R.id.vLineH);
		
		if (isSingleButton && mBtnCancel != null && mBtnOk != null ) {
			vLineV.setVisibility(View.GONE);
			mBtnCancel.setVisibility(View.GONE);
//			mBtnOk.setBackgroundResource(R.drawable.selector_btn_light_gray_radius);
		}

		WindowManager windowManager = (WindowManager) this.getContext()
				.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics dm = new DisplayMetrics();
		windowManager.getDefaultDisplay().getMetrics(dm);

		getWindow().getAttributes().width = dm.widthPixels - dip2px(this.getContext(), 60);
		getWindow().getAttributes().dimAmount = 0.3f;
//		getWindow().getAttributes().y = -dm.heightPixels * 1 / 10;

		this.setCancelable(true);
		this.setCanceledOnTouchOutside(true);
	}

	public Object getData() {
		return mData;
	}

	public void setData(Object data) {
		this.mData = data;
	}

	public void setDialogContent(String content) {
		mContent = content;
		if (mTvContent != null) {
			mTvContent.setText(mContent);
		}
	}
	
	public void setDialogContent(String content,float textSize) {
		mContent = content;
		if (mTvContent != null) {
			mTvContent.setText(mContent);
			mTvContent.setTextSize(textSize);
		}
	}
	
	public void setContentGravity(int gravity) {
		if (mTvContent != null) {
			mTvContent.setGravity(gravity);
		}
	}

	public void isSingleButton(boolean isSingle) {
		isSingleButton = isSingle;
		if (isSingle) {
			if (mBtnCancel != null && mBtnOk != null) {
				vLineV.setVisibility(View.GONE);
				mBtnCancel.setVisibility(View.GONE);
			}
		} else {
			if (mBtnCancel != null && mBtnOk != null) {
				vLineV.setVisibility(View.VISIBLE);
				mBtnCancel.setVisibility(View.VISIBLE);
			}
		}
	}
	public void setSingleButtonText(String text){
		mBtnOkText=text;
		if (isSingleButton) {
			mBtnOk.setText(text);
		}
	}
	
	public void setButtonText(String left, String right) {
		mBtnCancelText=left;
		mBtnOkText=right;
		if (mBtnCancel != null && mBtnOk != null) {
			mBtnCancel.setText(left);
			mBtnOk.setText(right);
		}
	}
	
	public void diyBtnTextColor(int color) {
		if (mBtnCancel != null && mBtnOk != null) {
			mBtnCancel.setTextColor(color);
			mBtnOk.setTextColor(color);
		}
	}
	
	public void diyDefaultBtnStyle() {
		if (mBtnCancel != null && mBtnOk != null) {
			mBtnCancel.setBackgroundColor(Color.TRANSPARENT);
			mBtnOk.setBackgroundColor(Color.TRANSPARENT);
			mBtnCancel.setTextColor(Color.BLACK);
			mBtnOk.setTextColor(Color.BLACK);
			vLineV.setBackgroundColor(Color.rgb(200, 200, 200));
			vLineH.setBackgroundColor(Color.rgb(200, 200, 200));
		}
	}
	
	private int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	public void setSubContent(String content) {
		if (mTvSubContent != null) {
			mTvSubContent.setVisibility(View.VISIBLE);
			mTvSubContent.setText(content);
		}
	}
}
